$(function () {
    function isempty(input, msg) {
        if (input.val() == '') {
            $('.jm_error').html('');
            input.next().html(msg).show();
            return false;
        }
        return true;
    }

//获取短信验证码
    var isPhone = /(?:\(\d{3,4}\)|\d{3,4}-?)\d{8}/;//手机号码验证
    $('.code-btn').click(function () {
        $('.jm_error').html('');
        if (!isempty($("input[name='uname']"), '请输入手机号码')) return false;
        var mobile = $("input[name='uname']").val();
        if (!isPhone.test(mobile)) {
            $('input[name="uname"]').next().html('手机号码格式不正确');
            return false;
        }
        //ajax提交获取
        url = AJAX_URL + '/user/sendmessages';
        $.ajax({
            url: url,
            type: 'GET',
            dataType: 'json',
            data: {phone: mobile},
            success: function (data) {
                if (data.code == 0) {
                    var mytime = myDate.getTime(); //获取当前时间戳
                    var endMytime = mytime + 1000 * 60; //计算结束时间戳
                    var phone = $("#phone").val();
                    $.cookie("endMytime", endMytime, {expires: endMytime});
                    $.cookie("phone", phone);
                    maxtime = 59;
                    var phone = $('#phone').val();
                    timer = setInterval(CountDown, 1000);
                    $("#getcode").hide();
                    $("#timer").html('60秒后可再次发送').show();
                    $(".phone-tips").show();
                    maxtime_phone = 5;
                    timer_phone = setInterval(CountDown_phone, 1000);
                    layer.alert(data.message, {
                        icon: 0
                    });

                } else {
                    $('input[name="uname"]').next().html(data.message);

                }
                ;
            }
        })

    })


    //表单提交
    $('#frm-post').submit(function () {
        $('.jm_error').html('');
        if (!isempty($("input[name='uname']"), '请输入手机号码')) return false;
        var mobile = $("input[name='uname']").val();
        if (!isPhone.test(mobile)) {
            $('input[name="uname"]').next().html('手机号码格式不正确');
            return false;
        }
        if (!isempty($("input[name='captcha']"), '请输入短信验证码')) return false;
        //ajax提交验证
        $.ajax({
            url: $(this).attr('action'),
            type: 'POST',
            dataType: 'json',
            data: $(this).serializeArray(),
            success: function (data) {
                if (data.code == 0) {
                    var url = AJAX_URL + '/user/forgettwo?phone=' + $("input[name='uname']").val() + '&code=' + $('#captcha').val();
                    window.location.href = url;

                } else {
                    $("input[name='captcha']").next().html(data.message);
                }
                ;
            }
        })


    })


    //获取短信验证码倒计时
    var myDate = new Date();
    var newTime = myDate.getTime();
    var timePoor = $.cookie("endMytime") - newTime;
    var maxtime_phone = 0;
    var timer_phone;
    if (timePoor < 0) {
        $.cookie("endMytime", null); //使cookie失效
    }
    var maxtime;
    maxtime = parseInt(timePoor / 1000);
    var timer;
    if (maxtime > 0) {
        $("#phone").val($.cookie("phone"));
        CountDown();
        timer = setInterval(CountDown, 1000);
    }

    function CountDown() {
        if (maxtime >= 0) {
            $("#getcode").hide();
            $("#timer").show();
            seconds = Math.floor(maxtime % 60);
            msg = seconds + "秒后可再次发送";
            $("#timer").html(msg);
            $("#getcode").val(msg);
            if (maxtime < 56) {
                $(".phone-tips").hide();
            }
            --maxtime;
            window.name = maxtime;
        } else {
            clearInterval(timer);
            $("#getcode").show();
            $("#timer").hide();
            $("#getcode").val("点击获取短信验证码");
        }
    }

    function CountDown_phone() {
        if (maxtime_phone > 0) {
            seconds_phone = Math.floor(maxtime_phone % 60);
            $("#timephone").html(maxtime_phone);

            --maxtime_phone;
            window.name = maxtime_phone;
        } else {
            clearInterval(timer_phone);
            $(".phone-tips").hide();
        }
    }

    //获取短信验证码倒计时结束
})